// ContentView.swift
import SwiftUI
import FaceAISDK_Core
import ToastUI


/**
 * iOS  FaceAISDK 功能导航页面
 */
struct FaceAINaviView: View {
    @State private var navigationPath = NavigationPath()
    @State private var addFaceResult: String?
    @State private var faceVerifyResult: FaceVerifyResult?
    
    //录入保存的FaceID 值。一般是你的业务体系中个人的唯一编码，比如账号 身份证
    private let faceID="yourFaceIDValue";
    
    var body: some View {
        NavigationStack(path: $navigationPath) {
            ZStack {
                Color.faceMain.ignoresSafeArea()
                VStack(spacing: 20) {
                    
                    //录入你的人脸图片
                    Button("Add Your Face Image") {
                        navigationPath.append(FaceAINaviDestination.AddFacePageView(faceID))
                    }
                    .font(.system(size: 20).bold())
                    .controlSize(.large)
                    .foregroundColor(Color.white)
                    .padding(.top,50)
                    
                    Text("\(addFaceResult ?? " ")")
                        .font(.system(size: 8).bold())
                        .foregroundColor(Color.white)
                        .padding(.horizontal,7)
                    
                    //人脸识别+活体检测
                    Button("Face Verify and Liveness Detection") {
                        navigationPath.append(FaceAINaviDestination.VerifyFacePageView(faceID))
                    }
                    .font(.system(size: 20).bold())
                    .foregroundColor(Color.white)
                    .padding(.top,15)
                    
                    //仅动作活体检测
                    Button("ONLY Motion Liveness Detection") {
                        navigationPath.append(FaceAINaviDestination.LivenessView(faceID))
                    }
                    .font(.system(size: 20).bold())
                    .foregroundColor(Color.white)
                    .padding(.top,25)
                    
                    //判断人脸图片是否存在
                    Button("is Face Image Exist") {
                        print("isFaceImageExist？ ：\(FaceImageManger.isFaceImageExist(faceID: faceID))")
                    }
                    .font(.system(size: 18).bold())
                    .foregroundColor(Color.white)
                    .padding(.top,33)

                    //人脸图片转为Base64
                    Button("Convert Face Image to Base64") {
                        print("Base64：\(String(describing: FaceImageManger.imageFromDocumentsToBase64(fileName: faceID)))")
                    }
                    .foregroundColor(Color.white)
                    .padding(.top,11)
                    .font(.system(size: 18).bold())

                    //保存Base64人脸图片
                    Button("Save Base64 Face Images") {
                        print("Save Path：\(String(describing: FaceImageManger.saveBase64ImageToLocal(base64String: base64Data, fileName: faceID)))")
                    }
                    .foregroundColor(Color.white)
                    .padding(.top,11)
                    .font(.system(size: 18).bold())

                    Spacer()
                    
                    Button("About us"){
                        // 记得切换成iOS 的介绍版本
                        let url = URL(string: "https://mp.weixin.qq.com/s/R43s70guLqxA6JPEdWtjcA")
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                            if UIApplication.shared.canOpenURL(url!) {
                                UIApplication.shared.open(url!)
                            }
                        }
                    }
                    .foregroundColor(Color.white)
                    .font(.system(size: 16).bold())

                }
            }
            .navigationTitle("🧭 FaceAISDK")
            .navigationDestination(for: FaceAINaviDestination.self) { destination in
                switch destination {
                    
                case .AddFacePageView(let param):
                    AddFaceView(faceID: param,onDismiss: { result in
                        addFaceResult = result
                        if !navigationPath.isEmpty { // 检查路径是否为空
                            navigationPath.removeLast()
                        }
                    })
                    
                case .VerifyFacePageView(let param):
                    //设置的相似度阈值threshold越高，对人脸角度，环境光线和摄像头宽动态要求越高
                    VerifyFaceView(faceID: param,threshold: 0.85, onDismiss: { result in
                        faceVerifyResult = result
                        
                        // faceVerifyResult.code
                        // -2  人脸识别动作活体检测超过10秒
                        // -1  多次切换人脸或检查失败
                        // 0   默认值
                        // 1   人脸识别对比成功大于设置的threshold
                        // 2   人脸识别对比识别小于设置的threshold
                        print("verifyFaceView Result ：\(faceVerifyResult?.tips) \(faceVerifyResult?.code)")
                        
                        if !navigationPath.isEmpty { // 检查路径是否为空
                            navigationPath.removeLast()
                        }
                    })
                case .LivenessView(let param):
                    
                    // faceVerifyResult.code
                    // -2  人脸识别动作活体检测超过10秒
                    // -1  多次切换人脸或检查失败
                    // 0   默认值
                    // 3   动作活体检测成功
                    LivenessDetectView(faceID: param,onDismiss: { result in
                        print("Motion Liveness Result：\(result.tips) \(result.code)")
                        if !navigationPath.isEmpty { // 检查路径是否为空
                            navigationPath.removeLast()
                        }
                    })
                    
                }
            }
        }
        .onAppear {
            //和合适的场景，提前一点初始化FaceAISDK
            FaceAISDK.initSDK()
        }
    }
    
}

enum FaceAINaviDestination: Hashable {
    case AddFacePageView(String)
    case VerifyFacePageView(String)
    case LivenessView(String)
}

#Preview {
    FaceAINaviView()
}



// Base 64 编码图片
private let base64Data=""
